package com.it.server.controller.user;

import com.it.pojo.Entity.Dish;
import com.it.pojo.VO.DishDetailVO;
import com.it.pojo.VO.DishRotationVO;
import com.it.common.result.Result;
import com.it.server.service.DishService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/dish")
@Slf4j
@Tag(name="菜品接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/rotation/{merchant_id}")
    @Operation(summary="查询商家销量前三的数据")
    public Result<List<DishRotationVO>> rotation(@PathVariable Integer merchant_id){
        log.info("查询商家销量前三的数据");
        List<DishRotationVO> list = dishService.rotation(merchant_id);
        return Result.success(list);
    }
    @GetMapping("/list/{merchant_id}")
    @Operation(summary="查询商家的菜品列表")
    public Result<List<Dish>> list(@PathVariable Integer merchant_id){
        log.info("查询商家的菜品列表");
        String key = "dish"+merchant_id;
        if(redisTemplate.opsForValue().get(key) != null){
            return Result.success((List<Dish>) redisTemplate.opsForValue().get(key));
        }
        List<Dish> list = dishService.list(merchant_id);
        redisTemplate.opsForValue().set(key,list);
        return Result.success(list);
    }
    @GetMapping("/detail/{id}")
    @Operation(summary="查询菜品详情")
    public Result<DishDetailVO> detail(@PathVariable Integer id){
        log.info("查询菜品详情");
        DishDetailVO dish = dishService.detail(id);
        return Result.success(dish);
    }

}
